<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>iOS机器学习 | MoneyLee’s Blog</title><meta name="author" content="李财"><meta name="copyright" content="李财"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="机器学习 Machine Learing项目地址 ####机器学习的分类  有监督学习 – 准确,但是费时间 给机器一个对应关系 (指着一个手机 告诉机器他是手机) — 训练 (给训练数据贴标签label) 训练完成后 指着手机问它是什么 — 输入 告诉我们它是手机 — 输出 output  无监督学习 不给出对应关系 只是扔一堆东西给机器, 机器最终只能辨别出哪些东西是一类, 哪些不是一类, 仅">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS机器学习">
<meta property="og:url" content="https://moneylee.gitee.io/blog/2020/08/21/iOS%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="MoneyLee’s Blog">
<meta property="og:description" content="机器学习 Machine Learing项目地址 ####机器学习的分类  有监督学习 – 准确,但是费时间 给机器一个对应关系 (指着一个手机 告诉机器他是手机) — 训练 (给训练数据贴标签label) 训练完成后 指着手机问它是什么 — 输入 告诉我们它是手机 — 输出 output  无监督学习 不给出对应关系 只是扔一堆东西给机器, 机器最终只能辨别出哪些东西是一类, 哪些不是一类, 仅">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl0019wlj31ej0u0x6q.jpg">
<meta property="article:published_time" content="2020-08-21T02:04:10.000Z">
<meta property="article:modified_time" content="2020-08-21T09:33:38.000Z">
<meta property="article:author" content="李财">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl0019wlj31ej0u0x6q.jpg"><link rel="shortcut icon" href="/blog/img/favicon.png"><link rel="canonical" href="https://moneylee.gitee.io/blog/2020/08/21/iOS%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'iOS机器学习',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-08-21 17:33:38'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/blog/atom.xml" title="MoneyLee’s Blog" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2019/11/10/SYvXMgCFuTNOVeZ.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">47</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">9</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fa fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/bbs/"><i class="fa-fw fa fa-coffee"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-list"></i><span> 影音</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/music"><i class="fa-fw /music/"></i><span> 0</span></a></li><li><a class="site-page child" href="/blog/movies"><i class="fa-fw /movies/"></i><span> 1</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl0019wlj31ej0u0x6q.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">MoneyLee’s Blog</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fa fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/bbs/"><i class="fa-fw fa fa-coffee"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-list"></i><span> 影音</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/music"><i class="fa-fw /music/"></i><span> 0</span></a></li><li><a class="site-page child" href="/blog/movies"><i class="fa-fw /movies/"></i><span> 1</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">iOS机器学习</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-08-21T02:04:10.000Z" title="发表于 2020-08-21 10:04:10">2020-08-21</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2020-08-21T09:33:38.000Z" title="更新于 2020-08-21 17:33:38">2020-08-21</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/iOS/">iOS</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="iOS机器学习"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h3 id="机器学习-Machine-Learing"><a href="#机器学习-Machine-Learing" class="headerlink" title="机器学习 Machine Learing"></a>机器学习 Machine Learing</h3><p><a target="_blank" rel="noopener" href="https://gitee.com/moneylee/iosml-learn">项目地址</a></p>
<p>####机器学习的分类</p>
<ul>
<li><p>有监督学习 – 准确,但是费时间</p>
<p>给机器一个对应关系 (指着一个手机 告诉机器他是手机) — 训练 (给训练数据贴标签label)</p>
<p>训练完成后 指着手机问它是什么 — 输入</p>
<p>告诉我们它是手机 — 输出 output</p>
</li>
<li><p>无监督学习</p>
<p>不给出对应关系 只是扔一堆东西给机器, 机器最终只能辨别出哪些东西是一类, 哪些不是一类, 仅此而已</p>
</li>
<li><p>半监督学习</p>
</li>
<li><p>增强学习  alphaGo</p>
</li>
</ul>
<h4 id="使用苹果提供的Core-ML2"><a href="#使用苹果提供的Core-ML2" class="headerlink" title="使用苹果提供的Core ML2"></a>使用苹果提供的Core ML2</h4><p>苹果也提供了一些训练好的数据,可以在项目中直接使用</p>
<p><a target="_blank" rel="noopener" href="https://developer.apple.com/machine-learning/">苹果机器学习官网地址</a></p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghy6yv9kidj313o0tajuz.jpg" alt="image-20200821094834841"></p>
<p>目前苹果开放的一些可用的人工智能库</p>
<p>我们先用官网提供的一些训练模型尝试下使用,再去网上找一些Pyton训练好的模型来使用</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghya6vqc4zj31gm0u0h8n.jpg" alt="image-20200821114003817"></p>
<p>我们在官网找到想要使用的模型 MobileNetV2FP16</p>
<p>然后下载到本地 导入到项目中</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line">//</span><br><span class="line">//  ViewController.swift</span><br><span class="line">//  ML</span><br><span class="line">//</span><br><span class="line">//  Created by MoneyLee on 2020/8/21.</span><br><span class="line">//  Copyright © 2020 MoneyLee. All rights reserved.</span><br><span class="line">//</span><br><span class="line"></span><br><span class="line">import UIKit</span><br><span class="line">//机器学习必须引用的</span><br><span class="line">import CoreML</span><br><span class="line">//视界 图像类的工具</span><br><span class="line">import Vision</span><br><span class="line"></span><br><span class="line">class ViewController: UIViewController &#123;</span><br><span class="line"></span><br><span class="line">    @IBOutlet weak var showImgV: UIImageView!</span><br><span class="line">    </span><br><span class="line">    override func viewDidLoad() &#123;</span><br><span class="line">        super.viewDidLoad()</span><br><span class="line">        // Do any additional setup after loading the view.</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @IBAction func camreClick(_ sender: UIBarButtonItem) &#123;</span><br><span class="line">        let picker = UIImagePickerController()</span><br><span class="line">        picker.delegate = self</span><br><span class="line">        picker.sourceType = .camera</span><br><span class="line">        present(picker, animated: true, completion: nil)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate &#123;</span><br><span class="line">    </span><br><span class="line">    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) &#123;</span><br><span class="line">        </span><br><span class="line">        if let img = info[.originalImage] as? UIImage &#123;</span><br><span class="line">            showImgV.image = img</span><br><span class="line">            navigationItem.title = &quot;识别中...&quot;</span><br><span class="line">            //转化为CIImage</span><br><span class="line">            guard let ciImg = CIImage(image: img) else &#123;</span><br><span class="line">                fatalError(&quot;不能把图像转化为CIImage&quot;)</span><br><span class="line">            &#125;</span><br><span class="line">            //加载训练好的模型</span><br><span class="line">            guard let model = try? VNCoreMLModel(for: MobileNetV2FP16().model) else &#123;</span><br><span class="line">                fatalError(&quot;加载ML Model失败&quot;)</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">            //创建识别请求</span><br><span class="line">            let request = VNCoreMLRequest(model: model) &#123; (req, error) in</span><br><span class="line">                //请求的回调 res 识别的结果</span><br><span class="line">                guard let res = req.results else &#123;</span><br><span class="line">//                    fatalError(&quot;图像识别失败&quot;)</span><br><span class="line">                    self.navigationItem.title = &quot;图片识别失败&quot;</span><br><span class="line">                    return</span><br><span class="line">                &#125;</span><br><span class="line">                </span><br><span class="line">                let classifcations = res as! [VNClassificationObservation]</span><br><span class="line">                if classifcations.isEmpty &#123;</span><br><span class="line">                    print(&quot;没有识别成功&quot;)</span><br><span class="line">                    self.navigationItem.title = &quot;没有识别成功&quot;</span><br><span class="line">                &#125; else &#123;</span><br><span class="line">                    //取识别概率最大的结果</span><br><span class="line">                    print(classifcations.first!.identifier)</span><br><span class="line">                    self.navigationItem.title = classifcations.first!.identifier</span><br><span class="line">                &#125;</span><br><span class="line">                </span><br><span class="line">            &#125;</span><br><span class="line">            //苹果的model图像是正方型, 我们将获取的图像转换为正方型 有助于识别, 默认是原图</span><br><span class="line">            request.imageCropAndScaleOption = .centerCrop</span><br><span class="line">            </span><br><span class="line">            //执行识别请求</span><br><span class="line">            do &#123;</span><br><span class="line">                try VNImageRequestHandler(ciImage: ciImg).perform([request])</span><br><span class="line">            &#125; catch &#123;</span><br><span class="line">                print(&quot;执行图像识别请求失败, 原因是: \(error.localizedDescription)&quot;)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        picker.dismiss(animated: true, completion: nil)   </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghybjbcgysj30u01szaf0.jpg" alt="6401597981163_.pic"></p>
<p><a target="_blank" rel="noopener" href="https://gitee.com/moneylee/iosml-learn">项目地址</a></p>
<hr>
<h4 id="如何转化网上第三方的Model转化为MLModel-供iOS使用"><a href="#如何转化网上第三方的Model转化为MLModel-供iOS使用" class="headerlink" title="如何转化网上第三方的Model转化为MLModel 供iOS使用"></a>如何转化网上第三方的Model转化为MLModel 供iOS使用</h4><p>使用官网提供的 ML Tools转化三方Model</p>
<p><a target="_blank" rel="noopener" href="https://developer.apple.com/machine-learning/resources/">下载工具地址</a></p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghybx3inqsj31jy0ss44r.jpg" alt="image-20200821123952650"></p>
<p><a target="_blank" rel="noopener" href="https://coremltools.readme.io/docs">文档地址</a></p>
<p>使用coremltools Python软件包将第三方培训库（例如<a target="_blank" rel="noopener" href="https://www.tensorflow.org/">TensorFlow</a>和<a target="_blank" rel="noopener" href="https://pytorch.org/">PyTorch）的模型</a>转换为<a target="_blank" rel="noopener" href="https://developer.apple.com/documentation/coreml">Core ML</a>格式。然后，您可以使用Core ML将模型集成到您的应用程序中。</p>
<p>Core ML为所有模型提供统一的表示形式。您的应用程序使用Core ML API和用户数据在用户设备上进行预测并微调模型。严格在用户设备上运行模型将消除对网络连接的任何需求，这有助于保持用户数据的私密性和您的应用程序的响应速度。</p>
<p>Core ML通过利用CPU，GPU和神经引擎来优化设备上的性能，同时最大程度地减少其内存占用空间和功耗。</p>
<h3 id=""><a href="#" class="headerlink" title=""></a><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghybyk2f4vj30wg0mudlh.jpg" alt="截屏2020-08-21 下午12.41.12"></h3><h2 id="什么是coremltools？"><a href="#什么是coremltools？" class="headerlink" title="什么是coremltools？"></a>什么是coremltools？</h2><p>该<a target="_blank" rel="noopener" href="https://github.com/apple/coremltools">coremltools Python包</a>是第三方模型转换到核心ML格式的主要方式。</p>
<p>使用coremltools，您可以执行以下操作：</p>
<ul>
<li>将训练有素的模型从<a target="_blank" rel="noopener" href="https://www.tensorflow.org/">TensorFlow</a>和<a target="_blank" rel="noopener" href="https://pytorch.org/">PyTorch</a>等框架转换为Core ML格式。</li>
<li>读取，写入和优化Core ML模型。</li>
<li>通过使用Core ML进行预测来验证macOS中的转换/创建。</li>
</ul>
<p>您可以将训练有素的模型从以下框架转换为Core ML格式：</p>
<table>
<thead>
<tr>
<th align="left">模特家族</th>
<th align="left">支持包</th>
</tr>
</thead>
<tbody><tr>
<td align="left">神经网络</td>
<td align="left"><a target="_blank" rel="noopener" href="https://www.tensorflow.org/versions/r1.15/api_docs/python/tf">TensorFlow 1（1.14.0+）</a> <a target="_blank" rel="noopener" href="https://www.tensorflow.org/">TensorFlow 2（2.1.0+）</a> <a target="_blank" rel="noopener" href="https://pytorch.org/">PyTorch（1.4.0+，&lt;= 1.6.0）</a> <a target="_blank" rel="noopener" href="https://keras.io/">Keras（2.0.4+）</a> <a target="_blank" rel="noopener" href="https://github.com/onnx/onnx">ONNX（1.6.0）</a> <a target="_blank" rel="noopener" href="https://caffe.berkeleyvision.org/">Caffe（1.0）</a></td>
</tr>
<tr>
<td align="left">树木乐团</td>
<td align="left"><a target="_blank" rel="noopener" href="https://xgboost.readthedocs.io/en/latest/index.html">XGboost（1.1.0）</a> <a target="_blank" rel="noopener" href="https://scikit-learn.org/stable/">scikit学习（0.18.1）</a></td>
</tr>
<tr>
<td align="left">广义线性模型</td>
<td align="left"><a target="_blank" rel="noopener" href="https://scikit-learn.org/stable/">scikit学习（0.18.1）</a></td>
</tr>
<tr>
<td align="left">支持向量机</td>
<td align="left"><a target="_blank" rel="noopener" href="https://pypi.org/project/libsvm/">LIBSVM（3.22）</a> <a target="_blank" rel="noopener" href="https://scikit-learn.org/stable/">scikit学习（0.18.1）</a></td>
</tr>
<tr>
<td align="left">管道（预处理和后处理）</td>
<td align="left"><a target="_blank" rel="noopener" href="https://scikit-learn.org/stable/">scikit学习（0.18.1）</a></td>
</tr>
</tbody></table>
<h4 id="开始安装-Python-pip-安装coremltools"><a href="#开始安装-Python-pip-安装coremltools" class="headerlink" title="开始安装 Python+pip+安装coremltools"></a>开始安装 Python+pip+安装coremltools</h4><p>coremltools支持Python 2和Python3。我们建议您使用<a target="_blank" rel="noopener" href="https://docs.conda.io/en/latest/index.html">Conda</a>或<a target="_blank" rel="noopener" href="https://virtualenv.pypa.io/en/latest/">virtualenv</a>安装Python 3.6或更高版本。</p>
<blockquote>
<h3 id="📘Python-2-7支持"><a href="#📘Python-2-7支持" class="headerlink" title="📘Python 2.7支持"></a>📘Python 2.7支持</h3><p>由于<a target="_blank" rel="noopener" href="https://www.python.org/doc/sunset-python-2/">Python</a>核心团队不再支持<a target="_blank" rel="noopener" href="https://www.python.org/doc/sunset-python-2/">Python 2</a>，因此我们建议使用Python 3以获得coremltools的最佳体验。到2020年底，我们将继续完全支持Python 2.7，但是coremltools的未来版本可能不支持Python 2.7。请相应地计划您的迁移。</p>
</blockquote>
<p>跟着这些步骤：</p>
<ol>
<li>安装<a target="_blank" rel="noopener" href="https://pip.pypa.io/en/stable/">pip</a>软件包安装程序，以便可以使用它安装coremltools。</li>
<li>安装Python软件包管理器以为coremltools创建一个Python环境。我们建议使用<a target="_blank" rel="noopener" href="https://docs.conda.io/en/latest/index.html">Conda，</a>因为它是安装所有必需依赖项（例如numpy）的最可靠方法。您可以改为使用<a target="_blank" rel="noopener" href="https://virtualenv.pypa.io/en/latest/">virtualenv</a>。</li>
<li>使用为其提供的软件包指南安装用于转换的第三方源软件包（例如<a target="_blank" rel="noopener" href="https://www.tensorflow.org/">TensorFlow</a>和<a target="_blank" rel="noopener" href="https://pytorch.org/">PyTorch</a>）。该coremltools包中<em>不</em>包括第三方源代码包。</li>
</ol>
<p>python正常有自带的 但是版本低 建议升级下 再安装个pip包管理工具  都自行百度吧 很简单</p>
<p>安装转换工具 coremltools</p>
<p><code>sudo pip3 install -U coremltools</code></p>
<p>我的是pip3</p>
<h4 id="查找共享模型"><a href="#查找共享模型" class="headerlink" title="查找共享模型"></a>查找共享模型</h4><p><a target="_blank" rel="noopener" href="https://github.com/BVLC/caffe/wiki/Model-Zoo">caffe提供的开源模型</a></p>
<p>找到可以识别各种花的模型 <a target="_blank" rel="noopener" href="https://github.com/BVLC/caffe/wiki/Model-Zoo#caffenet-fine-tuned-for-oxford-flowers-dataset">CaffeNet fine-tuned for Oxford flowers dataset</a></p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghyd93jkb7j31oc0swtgp.jpg" alt="image-20200821132600552"></p>
<p>找到其网站上的下载地址 可能需要翻墙 或者下载比较缓慢</p>
<p><strong>比较大 推荐使用下载工具 Motrix</strong></p>
<p> 我们最终需要三个文件</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghydcn15b1j31gs0oqgqw.jpg" alt="image-20200821132924228"></p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghydd4a6ofj31cy0nwwhs.jpg" alt="image-20200821132952355"></p>
<p>我们获得下面这3个文件</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1ghydyj5al0j30k2090t9c.jpg" alt="image-20200821135022001"></p>
<p>我们需要对其中的 class_labels.py修改下  我们需要一个.txt文件 并且内部的数据需要整理成如下格式</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghyek3i795j311w0n8add.jpg" alt="image-20200821141109996"></p>
<p>我们可以把前后的多余字符去掉,然后利用字符串分隔,再通过控制台打印的方式快速得到这个数据,然后复制到文件内,可以使用我demo内的StringTool工具来处理</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> UIKit</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">StringTool</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">printUsigString</span>(<span class="params">fileName</span>: <span class="type">String</span>, <span class="params">fileType</span>: <span class="type">String</span>)</span> -&gt; <span class="type">String</span> &#123;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">let</span> path <span class="operator">=</span> <span class="type">Bundle</span>.main.path(forResource: fileName, ofType: fileType)</span><br><span class="line">        <span class="keyword">let</span> url <span class="operator">=</span> <span class="type">URL</span>(fileURLWithPath: path<span class="operator">!</span>)</span><br><span class="line">        <span class="keyword">do</span> &#123;</span><br><span class="line">            <span class="keyword">let</span> data <span class="operator">=</span> <span class="keyword">try</span> <span class="type">Data</span>(contentsOf: url)</span><br><span class="line">            <span class="keyword">let</span> str <span class="operator">=</span>  <span class="type">NSString</span>(data:data ,encoding: <span class="type">String</span>.<span class="type">Encoding</span>.utf8.rawValue)<span class="operator">!</span> <span class="keyword">as</span> <span class="type">String</span></span><br><span class="line">            <span class="keyword">let</span> result : <span class="type">Array</span> <span class="operator">=</span> str.components(separatedBy: <span class="string">&quot;&#x27;, &#x27;&quot;</span>)</span><br><span class="line">            <span class="keyword">for</span> item <span class="keyword">in</span> result &#123;</span><br><span class="line">                <span class="built_in">print</span>(item)</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> str</span><br><span class="line">            </span><br><span class="line">        &#125; <span class="keyword">catch</span> &#123;</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;读取本地数据出现错误: <span class="subst">\(error.localizedDescription)</span>&quot;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;获取失败&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>最终我们得到这3个文件</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghyenh1nqgj30ru0d2wfg.jpg" alt="image-20200821141425152"></p>
<p>oxford102.caffemodel: 被训练好的模型</p>
<p>class_labels: 转化输出</p>
<p>deploy.prototxt: 转化输入</p>
<p>我们该如何转换呢</p>
<p><a target="_blank" rel="noopener" href="https://coremltools.readme.io/reference/converterscaffeconvert">转化caffe模型</a></p>
<p>####coremltools.converters.caffe.convert</p>
<ul>
<li><p><code>coremltools.converters.caffe.``convert</code>（<em>模型</em>，<em>image_input_names = []</em> ，<em>is_bgr = 假</em>，<em>red_bias = 0.0</em>，<em>blue_bias = 0.0</em>，<em>green_bias = 0.0</em>，<em>gray_bias = 0.0</em>，<em>image_scale = 1.0</em>，<em>class_labels = 无</em>，<em>predicted_feature_name = 无</em>，<em>model_precision = ‘FLOAT32’</em> ）</p>
<p>将Caffe模型转换为Core ML格式。参量<strong>型号：str | （str，str）| （str，str，str）| （str，str，dict）</strong>经过训练的Caffe神经网络模型，可以表示为：磁盘上经过训练的Caffe模型（.caffemodel）的路径两个路径的元组，其中第一个路径是.caffemodel文件的路径，第二个路径是deploy.prototxt的路径。三个路径的元组，其中第一个路径是训练后的.caffemodel文件的路径，第二个路径是deploy.prototxt的路径，而第三个路径是平均图像二进制文件的路径，从输入中减去数据图像作为预处理步骤。.caffemodel和.prototxt的两个路径的元组，以及以图像输入名称为键的dict和以图像binaryprotos为值的路径。键应与通过参数“ image_input_name”提供的输入名称相同。</p>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">coremltools.converters.caffe.convert(model, image_input_names=[], class_labels=None, predicted_feature_name=None, model_precision=&#x27;float32&#x27;)</span><br></pre></td></tr></table></figure>

<p><code>image_input_names=[]</code></p>
<p>这个参数是什么呢 打开deploy.prototxt文件</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghyf54cd0cj30z40kymzp.jpg" alt="image-20200821143122524"></p>
<p>这里的input对应的参数就是我们要传入的</p>
<p><code>class_labels=None</code></p>
<p>这个参数放什么呢 就是我们class_labels文件的路径</p>
<p>再python环境下执行转换操作</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">Python 3.7.9 (v3.7.9:13c94747c7, Aug 15 2020, 01:31:08) </span><br><span class="line">[Clang 6.0 (clang-600.0.57)] on darwin</span><br><span class="line">&gt;&gt;&gt; import coremltools</span><br><span class="line">&gt;&gt;&gt; </span><br><span class="line">&gt;&gt;&gt; coreml_model = coremltools.converters.caffe.convert((&#x27;oxford102.caffemodel&#x27;,&#x27;deploy.prototxt&#x27;), image_input_names=&#x27;data&#x27;, class_labels=&#x27;class_labels.txt&#x27;)</span><br><span class="line"></span><br><span class="line">================= Starting Conversion from Caffe to CoreML ======================</span><br><span class="line">Layer 0: Type: &#x27;Input&#x27;, Name: &#x27;input&#x27;. Output(s): &#x27;data&#x27;.</span><br><span class="line">Ignoring batch size and retaining only the trailing 3 dimensions for conversion. </span><br><span class="line">Layer 1: Type: &#x27;Convolution&#x27;, Name: &#x27;conv1&#x27;. Input(s): &#x27;data&#x27;. Output(s): &#x27;conv1&#x27;.</span><br><span class="line">Layer 2: Type: &#x27;ReLU&#x27;, Name: &#x27;relu1&#x27;. Input(s): &#x27;conv1&#x27;. Output(s): &#x27;conv1&#x27;.</span><br><span class="line">Layer 3: Type: &#x27;Pooling&#x27;, Name: &#x27;pool1&#x27;. Input(s): &#x27;conv1&#x27;. Output(s): &#x27;pool1&#x27;.</span><br><span class="line">Layer 4: Type: &#x27;LRN&#x27;, Name: &#x27;norm1&#x27;. Input(s): &#x27;pool1&#x27;. Output(s): &#x27;norm1&#x27;.</span><br><span class="line">Layer 5: Type: &#x27;Convolution&#x27;, Name: &#x27;conv2&#x27;. Input(s): &#x27;norm1&#x27;. Output(s): &#x27;conv2&#x27;.</span><br><span class="line">Layer 6: Type: &#x27;ReLU&#x27;, Name: &#x27;relu2&#x27;. Input(s): &#x27;conv2&#x27;. Output(s): &#x27;conv2&#x27;.</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>注意在三个文件所在的文件夹路径内操作终端, 因为其中有个属性是文件路径, 因为我们在当前的文件夹内 所以可以输入文件名就行了</strong></p>
<p>指定完成后</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">================= Summary of the conversion: ===================================</span><br><span class="line">Detected input(s) and shape(s) (ignoring batch size):</span><br><span class="line">&#x27;data&#x27; : 3, 227, 227</span><br><span class="line"></span><br><span class="line">Network Input name(s): &#x27;data&#x27;.</span><br><span class="line">Network Output name(s): &#x27;prob&#x27;.</span><br><span class="line"></span><br><span class="line">&gt;&gt;&gt; </span><br></pre></td></tr></table></figure>

<p>给model取个名字</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; coreml_model.save(&#x27;oxford102.mlmodel&#x27;)</span><br></pre></td></tr></table></figure>

<p>查看文件内是否生成成功</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1ghyfvbcfpnj30n60dcmyf.jpg" alt="image-20200821145629077"></p>
<p>把这个模型导入到项目内</p>
<p>使用到的资源文件 因为下载比较慢可以在百度云下载</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">链接:https://pan.baidu.com/s/1_WXy4jiTOkusapYv37S3rw  密码:8bvu</span><br></pre></td></tr></table></figure>

<hr>
<h4 id="如何自己制作MLModel"><a href="#如何自己制作MLModel" class="headerlink" title="如何自己制作MLModel"></a>如何自己制作MLModel</h4><p>苹果为我们提供了这个方式</p>
<p><a target="_blank" rel="noopener" href="https://developer.apple.com/documentation/createml">创建ML</a></p>
<p>我们将要构建一个model 来区分狗和猫</p>
<p>首先我们需要获取大量狗和猫的图片</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghygqxiznaj30f40f2jss.jpg" alt="image-20200821152656460"></p>
<p>Test文件夹是存放我们用来验证结果的猫狗图片, 训练数据和测试数据分别占比 80% 20%</p>
<p>我们收集猫狗图片分别导入到4个文件夹内</p>
<p>训练及测试数据下载地址</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">链接:https://pan.baidu.com/s/1aUDT5HbXgjEOmAuOluY9DA  密码:rvae</span><br></pre></td></tr></table></figure>

<p>创建一个playground, 必须是mac的 而且左下角切换为手动执行模式</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghygva4qavj30os0de449.jpg" alt="image-20200821153106861"></p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghyhczmi8wj31o60n4wh4.jpg" alt="image-20200821154808500"></p>
<p>我们将两个包图片的文件夹整合到一个文件夹内拖拽到里面</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghyi0dqbr9j31l60u0q6m.jpg" alt="image-20200821161037029"></p>
<p>再将测试的数据文件夹同样拖拽到里面 就完成了制作</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghyi267at1j31l20u0wqj.jpg" alt="image-20200821161220775"></p>
<p>识别成功率100%</p>
<p>其实上面的方法即将被弃用了, 苹果已经在新版的XCode中集成了 create ML这个工具</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghyi6ty5a7j30ro0k2n6j.jpg" alt="image-20200821161648758"></p>
<p>通过这个工具更简单方便</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghyiadkf3uj31gp0u0thp.jpg" alt="image-20200821162013682"></p>
<p>然后点击上方的 Train开始训练</p>
<p>然后怎么将训练好的mode导出呢 我找了找没找到. 我勒个去. 只好这么做了, 找到工程文件, 右键显示包内容</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghyifgmd59j311g0kq43p.jpg" alt="image-20200821162506782"></p>
<p>这在里面呢</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghyig0a03ij314s0lsgoq.jpg" alt="image-20200821162538660"></p>
<p>为什么如此简单呢, CreateMLUI这个库利用苹果的照片app和siri等苹果产品优化好的机器学习架构和一些优化好的算法已经提供给了我们, 使我们训练模型变得异常简单.我们只需要使用这个平台就可以实现一些需求 比如汽车识别,车牌识别和表情识别等等</p>
<p>如何提高我们的准确率呢?</p>
<p>可以利用各种角度 各种光线将测试数据图片进行旋转 或者明暗调节</p>
<p>有很多网站已经为我们提供了测试数据集 datasets</p>
<p>比较出名的是 Kaggle 谷歌旗下的 各行各业的数据集供我们使用 <a target="_blank" rel="noopener" href="https://www.kaggle.com/datasets">地址</a></p>
<h4 id="自然语言Model的处理"><a href="#自然语言Model的处理" class="headerlink" title="自然语言Model的处理"></a>自然语言Model的处理</h4><p>我们这次搞一个自然语言的项目, 分辨好坏话, 比如美团的评论, 通过顾客的评语, 自动分类出是好评还是差评</p>
<p>但是因为中文的训练很麻烦, 对电脑的要求也很高,我们用英文进行编写</p>
<p>我们分辨是否是垃圾短信</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">import Cocoa</span><br><span class="line">import CreateML</span><br><span class="line"></span><br><span class="line">//总数据</span><br><span class="line">let data = try MLDataTable(contentsOf: URL(fileURLWithPath: &quot;/Users/moneylee/Documents/Temp/spam.csv&quot;))</span><br><span class="line"></span><br><span class="line">//把总数据分为训练数据和测试数据</span><br><span class="line">let (trainingData,testingData) = data.randomSplit(by: 0.8, seed: 5)</span><br><span class="line"></span><br><span class="line">//开始训练--并得出情感识别器（sentimentClassifier）-也就是训练出来的mlmodel</span><br><span class="line">let sentimentClassifier = try MLTextClassifier(trainingData: trainingData, textColumn: &quot;text&quot;, labelColumn: &quot;label&quot;)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">//设置mlmodel元数据</span><br><span class="line">let metadata = MLModelMetadata(author: &quot;Lebus&quot;, shortDescription: &quot;A model trained to classify spam SMS&quot;, version: &quot;1.0&quot;)</span><br><span class="line">//保持mlmodel</span><br><span class="line">try sentimentClassifier.write(to: URL(fileURLWithPath: &quot;/Users/moneylee/Documents/Temp/spam.mlmodel&quot;), metadata: metadata)</span><br><span class="line"></span><br><span class="line">//识别是否是垃圾短信</span><br><span class="line">try sentimentClassifier.prediction(from: &quot;hi,how are you?&quot;)</span><br><span class="line">try sentimentClassifier.prediction(from: &quot;Had your mobile 11 months or more? U R entitled to Update to the latest colour mobiles with camera for Free! Call The Mobile Update Co FREE on 08002986030&quot;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>如何使用</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> UIKit</span><br><span class="line"><span class="keyword">import</span> NaturalLanguage</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ViewController</span>: <span class="title">UIViewController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">override</span> <span class="function"><span class="keyword">func</span> <span class="title">viewDidLoad</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">super</span>.viewDidLoad()</span><br><span class="line">        <span class="comment">// Do any additional setup after loading the view.</span></span><br><span class="line">        </span><br><span class="line">        <span class="keyword">guard</span> <span class="keyword">let</span> model <span class="operator">=</span> <span class="keyword">try?</span> <span class="type">NLModel</span>(mlModel: spam().model) <span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="built_in">fatalError</span>(<span class="string">&quot;加载mlmodel失败&quot;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">let</span> str <span class="operator">=</span> model.predictedLabel(for: <span class="string">&quot;feel free to call 021-55558888 for more information&quot;</span>)&#123;</span><br><span class="line">            <span class="built_in">print</span>(str)</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://mlln.cn/2018/10/11/%E4%B8%AD%E6%96%87%E6%83%85%E6%84%9F%E5%88%86%E6%9E%90%E8%AF%AD%E6%96%99%E5%BA%93%E5%A4%A7%E5%85%A8-%E5%B8%A6%E4%B8%8B%E8%BD%BD%E5%9C%B0%E5%9D%80/">中文情感语录数据集</a></p>
<p>自然语言关联项目的数据集下载地址</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">链接:https://pan.baidu.com/s/1axNkcjwNE4bkpFPFJXYhLQ  密码:ubti</span><br></pre></td></tr></table></figure>



</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">李财</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://moneylee.gitee.io/blog/2020/08/21/iOS%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/">https://moneylee.gitee.io/blog/2020/08/21/iOS%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://moneylee.gitee.io/blog" target="_blank">MoneyLee’s Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl0019wlj31ej0u0x6q.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2021/09/14/Flutter%20%E5%90%AF%E5%8A%A8%E9%A1%B5%E9%85%8D%E7%BD%AE/"><img class="prev-cover" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info"></div></div></a></div><div class="next-post pull-right"><a href="/blog/2020/08/20/Docker%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E6%95%B4%E7%90%86/"><img class="next-cover" src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl4zhjzuj30qy0e0gyz.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Docker 基础知识整理</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2019/11/10/SYvXMgCFuTNOVeZ.png" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">李财</div><div class="author-info__description">嗨，我是李财 (MoneyLee)， 一名来自中国的 iOS开发者。</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">47</div></a></div><div class="card-info-data-item is-center"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">9</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/licai28" target="_blank" title=""><i class="fa fa-github"></i></a><a class="social-icon" href="mailto:licai_1024@hotmail.com" target="_blank" title=""><i class="fa fa-envelope"></i></a><a class="social-icon" href="/blog/img/mywechat.jpg" target="_blank" title=""><i class="fa fa-weixin"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0-Machine-Learing"><span class="toc-number">1.</span> <span class="toc-text">机器学习 Machine Learing</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E8%8B%B9%E6%9E%9C%E6%8F%90%E4%BE%9B%E7%9A%84Core-ML2"><span class="toc-number">1.1.</span> <span class="toc-text">使用苹果提供的Core ML2</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E8%BD%AC%E5%8C%96%E7%BD%91%E4%B8%8A%E7%AC%AC%E4%B8%89%E6%96%B9%E7%9A%84Model%E8%BD%AC%E5%8C%96%E4%B8%BAMLModel-%E4%BE%9BiOS%E4%BD%BF%E7%94%A8"><span class="toc-number">1.2.</span> <span class="toc-text">如何转化网上第三方的Model转化为MLModel 供iOS使用</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link"><span class="toc-number">2.</span> <span class="toc-text"></span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFcoremltools%EF%BC%9F"><span class="toc-number"></span> <span class="toc-text">什么是coremltools？</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BC%80%E5%A7%8B%E5%AE%89%E8%A3%85-Python-pip-%E5%AE%89%E8%A3%85coremltools"><span class="toc-number">0.1.</span> <span class="toc-text">开始安装 Python+pip+安装coremltools</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%F0%9F%93%98Python-2-7%E6%94%AF%E6%8C%81"><span class="toc-number">1.</span> <span class="toc-text">📘Python 2.7支持</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E6%89%BE%E5%85%B1%E4%BA%AB%E6%A8%A1%E5%9E%8B"><span class="toc-number">1.1.</span> <span class="toc-text">查找共享模型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E8%87%AA%E5%B7%B1%E5%88%B6%E4%BD%9CMLModel"><span class="toc-number">1.2.</span> <span class="toc-text">如何自己制作MLModel</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%87%AA%E7%84%B6%E8%AF%AD%E8%A8%80Model%E7%9A%84%E5%A4%84%E7%90%86"><span class="toc-number">1.3.</span> <span class="toc-text">自然语言Model的处理</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2021/09/14/Flutter%20%E5%90%AF%E5%8A%A8%E9%A1%B5%E9%85%8D%E7%BD%AE/" title="无题"><img src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="无题"/></a><div class="content"><a class="title" href="/blog/2021/09/14/Flutter%20%E5%90%AF%E5%8A%A8%E9%A1%B5%E9%85%8D%E7%BD%AE/" title="无题">无题</a><time datetime="2021-09-14T03:05:25.000Z" title="发表于 2021-09-14 11:05:25">2021-09-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/08/21/iOS%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/" title="iOS机器学习"><img src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl0019wlj31ej0u0x6q.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="iOS机器学习"/></a><div class="content"><a class="title" href="/blog/2020/08/21/iOS%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/" title="iOS机器学习">iOS机器学习</a><time datetime="2020-08-21T02:04:10.000Z" title="发表于 2020-08-21 10:04:10">2020-08-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/08/20/Docker%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E6%95%B4%E7%90%86/" title="Docker 基础知识整理"><img src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl4zhjzuj30qy0e0gyz.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Docker 基础知识整理"/></a><div class="content"><a class="title" href="/blog/2020/08/20/Docker%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E6%95%B4%E7%90%86/" title="Docker 基础知识整理">Docker 基础知识整理</a><time datetime="2020-08-20T12:12:02.000Z" title="发表于 2020-08-20 20:12:02">2020-08-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/08/19/Flutter/" title="Flutter"><img src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl0019wlj31ej0u0x6q.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Flutter"/></a><div class="content"><a class="title" href="/blog/2020/08/19/Flutter/" title="Flutter">Flutter</a><time datetime="2020-08-19T02:04:03.000Z" title="发表于 2020-08-19 10:04:03">2020-08-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/08/19/iOS%20AR%E5%92%8CARKit4/" title="iOS AR和ARKit4"><img src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl0019wlj31ej0u0x6q.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="iOS AR和ARKit4"/></a><div class="content"><a class="title" href="/blog/2020/08/19/iOS%20AR%E5%92%8CARKit4/" title="iOS AR和ARKit4">iOS AR和ARKit4</a><time datetime="2020-08-19T02:04:03.000Z" title="发表于 2020-08-19 10:04:03">2020-08-19</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By 李财</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>